package com.lagou.task18;

public class StoreHouse {
    //仓库产品容量
    private int cnt = 10;

    public synchronized void product(){
        //线程唤醒
        notify();
        if (cnt < 10){
            System.out.println("线程" + Thread.currentThread().getName() + "正在生产第" + (cnt+1) + "个产品");
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cnt++;
        }else {
            //线程阻塞
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
    public synchronized void consumer(){
        //线程唤醒
        notify();
        if (cnt > 0){
            System.out.println("线程" + Thread.currentThread().getName() + "正在消费第" + (cnt) + "个产品");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            cnt--;
        }else {
            //线程阻塞
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
